package org.budo.geo.location.service.tencent;

import java.nio.charset.Charset;

import org.budo.geo.location.service.api.BudoGeoLocation;
import org.budo.geo.location.service.api.BudoGeoLocationService;
import org.budo.support.http.client.Request;
import org.budo.support.http.client.Response;
import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 调用腾讯的接口，实现经纬度转地理位置
 * 
 * @author lmw
 */
public class TencentGeoLocationService implements BudoGeoLocationService {
    private static final Charset CHARSET_GBK = Charset.forName("GBK");

    private static final Logger log = Slf4j.getLogger();

    private static final String API_URL = "http://apis.map.qq.com/jsapi?qt=rgeoc";

    private static final String LNGLAT = "lnglat";

    @Override
    public BudoGeoLocation seekGeoLocation(Double longitude, Double latitude) {
        if (null == longitude || null == latitude) {
            log.error("#25 longitude=" + longitude + ", latitude=" + latitude + ", return null");
            return null;
        }

        Request request = new Request();
        request.setUrl(API_URL);
        request.setParameter(LNGLAT, longitude + "," + latitude);

        Response response = request.execute();
        String responseBody = new String(response.getResponseData(), CHARSET_GBK);

        return this.responseToGeoLocation(responseBody);
    }

    private BudoGeoLocation responseToGeoLocation(String responseBody) {
        if (null == responseBody || responseBody.isEmpty()) {
            log.error("#44 responseBody=" + responseBody);
            return null;
        }

        JSONObject jsonObject = JSON.parseObject(responseBody);
        JSONObject detailObject = jsonObject.getJSONObject("detail");
        if (null == detailObject || detailObject.isEmpty()) {
            log.error("#52 detailObject=" + detailObject + ", responseBody=" + responseBody);
            return null;
        }

        JSONArray poilistArray = detailObject.getJSONArray("poilist");
        if (null == poilistArray || poilistArray.isEmpty()) {
            log.error("#58 poilistArray=" + poilistArray + ", detailObject=" + detailObject + ", responseBody=" + responseBody);
            return null;
        }

        JSONObject poiObject = poilistArray.getJSONObject(0);
        JSONObject addrInfoObject = poiObject.getJSONObject("addr_info");
        if (null == addrInfoObject || addrInfoObject.isEmpty()) {
            log.error("#65 addrInfoObject=" + addrInfoObject + ", poiObject=" + poiObject + ", responseBody=" + responseBody);
            return null;
        }

        String country = "中国"; // TODO
        String region = addrInfoObject.getString("p");
        String city = addrInfoObject.getString("c");

        TencentGeoLocation geoLocation = new TencentGeoLocation();
        geoLocation.setCountry(country);
        geoLocation.setProvince(region);
        geoLocation.setCity(city);

        return geoLocation;
    }
}